package org.dromara.mp.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.dromara.common.core.domain.R;
import org.dromara.common.web.core.BaseController;
import org.dromara.mp.annotation.SwitchoverCheck;
import org.dromara.mp.service.WxMpQrCodeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 微信公众号二维码管理控制器
 *
 * @author ruoyi
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wx/mp/qrcode/{appid}")
public class WxMpQrCodeController extends BaseController {

    private final WxMpService wxMpService;
    private final WxMpQrCodeService wxMpQrCodeService;

    /**
     * 生成临时二维码
     *
     * @param appid         公众号appid
     * @param expireSeconds 过期时间（秒）
     * @param sceneStr      场景字符串
     * @return 二维码ticket
     */
    @PostMapping("/temp")
    @SwitchoverCheck
    public R<WxMpQrCodeTicket> createTempQrCode(@PathVariable String appid,
                                                @RequestParam(defaultValue = "2592000") int expireSeconds,
                                                @RequestParam int sceneStr) {
        try {
            WxMpQrCodeTicket ticket = wxMpQrCodeService.createTempQrCode(expireSeconds, sceneStr);
            return R.ok(ticket);
        } catch (Exception e) {
            log.error("生成临时二维码失败: {}", e.getMessage(), e);
            return R.fail("生成临时二维码失败: " + e.getMessage());
        }
    }

    /**
     * 生成永久二维码
     *
     * @param appid    公众号appid
     * @param sceneStr 场景字符串
     * @return 二维码ticket
     */
    @PostMapping("/forever")
    @SwitchoverCheck
    public R<WxMpQrCodeTicket> createForeverQrCode(@PathVariable String appid,
                                                   @RequestParam String sceneStr) {
        try {
            WxMpQrCodeTicket ticket = wxMpQrCodeService.createForeverQrCode(sceneStr);
            return R.ok(ticket);
        } catch (Exception e) {
            log.error("生成永久二维码失败: {}", e.getMessage(), e);
            return R.fail("生成永久二维码失败: " + e.getMessage());
        }
    }

    /**
     * 获取二维码URL
     *
     * @param appid  公众号appid
     * @param ticket 二维码ticket
     * @return 二维码URL
     */
    @GetMapping("/url")
    @SwitchoverCheck
    public R<String> getQrCodeUrl(@PathVariable String appid, @RequestParam String ticket) {
        try {
            String url = wxMpQrCodeService.getQrCodeUrl(ticket);
            return R.ok(url);
        } catch (Exception e) {
            log.error("获取二维码URL失败: {}", e.getMessage(), e);
            return R.fail("获取二维码URL失败: " + e.getMessage());
        }
    }

    /**
     * 生成活动二维码
     *
     * @param appid      公众号appid
     * @param activityId 活动ID
     * @return 二维码ticket
     */
    @PostMapping("/activity")
    @SwitchoverCheck
    public R<WxMpQrCodeTicket> createActivityQrCode(@PathVariable String appid,
                                                    @RequestParam String activityId) {
        try {
            WxMpQrCodeTicket ticket = wxMpQrCodeService.createActivityQrCode(activityId);
            return R.ok(ticket);
        } catch (Exception e) {
            log.error("生成活动二维码失败: {}", e.getMessage(), e);
            return R.fail("生成活动二维码失败: " + e.getMessage());
        }
    }

    /**
     * 生成渠道二维码
     *
     * @param appid     公众号appid
     * @param channelId 渠道ID
     * @return 二维码ticket
     */
    @PostMapping("/channel")
    @SwitchoverCheck
    public R<WxMpQrCodeTicket> createChannelQrCode(@PathVariable String appid,
                                                   @RequestParam String channelId) {
        try {
            WxMpQrCodeTicket ticket = wxMpQrCodeService.createChannelQrCode(channelId);
            return R.ok(ticket);
        } catch (Exception e) {
            log.error("生成渠道二维码失败: {}", e.getMessage(), e);
            return R.fail("生成渠道二维码失败: " + e.getMessage());
        }
    }
}
